package com.wanxi.education.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanxi.education.mapper.VideoMapper;
import com.wanxi.education.model.vo.video.*;
import com.wanxi.education.service.VideoService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 寇文宝
 * @Description:
 * @Date: Created in 2021/9/24
 * @Version: 1.0
 */
@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoMapper videoMapper;

    /**
     * 新增视频
     * @param videoAddVo 视频添加vo类，名字，uri，时长
     */
    @Override
    public void add(VideoAddVo videoAddVo) {
        videoMapper.add(videoAddVo);
    }

    /**
     * 后台视频管理页面分页查询+模糊查询
     * @param videoFuzzyQueryVo 当前页 ，查询多少条数据，视频名称，视频uri
     * @return
     */
    @Override
    public PageInfo<VideoGoBackVo> findPage(VideoFuzzyQueryVo videoFuzzyQueryVo) {
        PageHelper.startPage(videoFuzzyQueryVo.getPageNum(),videoFuzzyQueryVo.getPageSize());
        List<VideoGoBackVo> videoGoBackVoList=videoMapper.findPage(videoFuzzyQueryVo.getName(),videoFuzzyQueryVo.getUri());
        return new PageInfo<>(videoGoBackVoList);
    }

    /**
     * 通过id查询视频链接uri
     * @param id 视频id
     * @return
     */
    @Override
    public VideoFindByIdBackVo findById(Long id) {
        VideoFindByIdBackVo videoFindByIdBackVo=videoMapper.findById(id);
        return videoFindByIdBackVo;
    }

    /**
     * 视频更新
     * @param videoUpdateVo 视频更新vo类，id，name，uri
     */
    @Override
    public void update(VideoUpdateVo videoUpdateVo) {
        videoMapper.update(videoUpdateVo);
    }

    /**
     * 删除视频资源 ，当视频资源存在绑定信息时不能被删除
     * @param id 视频id
     * @param adminId 管理员id
     */
    @Override
    public int deleteById(Long id,Long adminId) {
        List<VideoBindCourseVo> videoBindCourseById = videoMapper.findVideoBindCourseById(id);
        if (ObjectUtils.isEmpty(videoBindCourseById)) {
            return videoMapper.deleteById(id, adminId);
        }
        return 0;
    }

    /**
     * 查看视频与哪些课程绑定
     * @param findVideoBindCourseVo 视频id，分页参数
     * @return 返回和视频绑定的课程列表
     */
    @Override
    public PageInfo<VideoBindCourseVo> findVideoBindCourseById(FindVideoBindCourseVo findVideoBindCourseVo) {
        PageHelper.startPage(findVideoBindCourseVo.getPageNum(),findVideoBindCourseVo.getPageSize());
        List<VideoBindCourseVo> videoBindCourseById = videoMapper.findVideoBindCourseById(findVideoBindCourseVo.getId());
        return new PageInfo<>(videoBindCourseById);
    }

}
